package homework1.test05;

import java.util.Arrays;
import java.util.Comparator;

public class BookTest {
    public static void main(String[] args) {
        // 创建Book数组
        Book[] books = new Book[5];

        // 创建多个Book对象并存储到数组中
        books[0] = new Book(1, "Java编程思想", "Bruce Eckel", 108.0, 15000, 3000);
        books[1] = new Book(2, "深入理解Java虚拟机", "周志明", 89.0, 23000, 5000);
        books[2] = new Book(3, "算法导论", "Thomas H. Cormen", 128.0, 8000, 1500);
        books[3] = new Book(4, "设计模式", "Erich Gamma", 99.0, 18000, 4000);
        books[4] = new Book(5, "数据结构与算法分析", "Mark Allen Weiss", 79.0, 12000, 2500);

        // 遍历并显示所有图书信息
        System.out.println("排序前的图书信息：");
        for (Book book : books) {
            System.out.println(book);
        }

        // 按照销量从高到低排序
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book b1, Book b2) {
                // 降序排列（从高到低）
                return Integer.compare(b2.getSales(), b1.getSales());
            }
        });

        // 遍历并显示排序后的图书信息
        System.out.println("\n按销量从高到低排序后的图书信息：");
        for (Book book : books) {
            System.out.println(book);
        }
    }
}
